package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/30 17:10
 **/
public class Day29_1_Op01_08_SetZeroes {
    class Solution {
        public void setZeroes(int[][] matrix) {
            if(matrix.length == 0 || matrix[0].length == 0) return;
            int m = matrix.length, n = matrix[0].length;
            boolean row = false, col = false;
            for(int i = 0; i < m; ++i){
                if(matrix[i][0] == 0) col = true;
            }
            for(int j = 0; j < n; ++j){
                if(matrix[0][j] == 0) row = true;
            }
            for(int i = 1; i < m; ++i){
                for(int j = 1; j < n; ++j){
                    if(matrix[i][j] == 0){
                        matrix[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                }
            }
            for(int i = 1; i < m; ++i){
                if(matrix[i][0] == 0){
                    for(int j = 0; j < n; ++j){
                        matrix[i][j] = 0;
                    }
                }
            }
            for(int j = 1; j < n; ++j){
                if(matrix[0][j] == 0){
                    for(int i = 0; i < m; ++i){
                        matrix[i][j] = 0;
                    }
                }
            }
            if(col){
                for(int i = 0; i < m; ++i) matrix[i][0] = 0;
            }
            if(row){
                for(int j = 0; j < n; ++j) matrix[0][j] = 0;
            }
        }
    }
}